package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.UpdatePasswordDTO;
import com.sky.mapper.UpdatePasswordMapper;
import com.sky.service.UpdatePasswordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class UpdatePasswordServiceImpl implements UpdatePasswordService {

    @Autowired
    private UpdatePasswordMapper updatePasswordMapper;

    @Override
    // 修改密码
    public boolean updatePassword(UpdatePasswordDTO updatePasswordDTO) {
        // 获取当前用户的ID
        String correctPassword = updatePasswordMapper.getPasswordById(BaseContext.getCurrentId());

        if (Objects.equals(correctPassword, updatePasswordDTO.getOldPassword())) { // 旧密码正确，则更新新密码
            updatePasswordMapper.updatePassword(BaseContext.getCurrentId(), updatePasswordDTO.getNewPassword());
            return true;
        } else {
            return false;
        }
    }
}
